﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SWEN_HMS.Classes;

namespace SWEN_HMS.StaffHousekeepingModule
{
    public partial class AddRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    IfRemarksIsYesOrNo();
                    List<int> allRoomNumList = StaffHousekeepingDBManager.getRoomNumList();
                    if (allRoomNumList != null)
                    {
                        roomNumList.DataSource = allRoomNumList;
                        roomNumList.DataBind();
                    }
                    else
                    {
                        roomNoLabel.Text = "No rooms available for selection!";
                    }
                }
            }
        }

        protected void remarksRadio_SelectedIndexChanged(object sender, EventArgs e)
        {
            IfRemarksIsYesOrNo();
        }

        protected void IfRemarksIsYesOrNo()
        {
            if (remarksRadio.SelectedValue == "Yes")
            {
                if (remarksTextbox.Enabled == false)
                {
                    remarksTextbox.Enabled = true;
                }
            }
            else
            {
                if (remarksTextbox.Enabled == true)
                {
                    remarksTextbox.Enabled = false;
                }
            }
        }

        protected void submitRequestBtn_Click(object sender, EventArgs e)
        {
            Request newReq = new Request();
            newReq.requestRemarks = "Reason: " + requestDropDown.SelectedValue + "<br>Remarks: " + remarksTextbox.Text;
            newReq.roomNo = Convert.ToInt32(roomNumList.SelectedValue);
            newReq.requestDateTime = DateTime.Parse(requestDateTextbox.Text + " " + requestTimeHoursDropDown.SelectedValue + ":" + requestTimeMinsTextbox.Text);
            int addReq = StaffHousekeepingDBManager.addRequest(newReq);

            if (addReq == 1)
            {
                indicationLabel.Text = "Request has been successfully added as Request ID <b><u>" + Convert.ToString(StaffHousekeepingDBManager.getLatestRequestID()) +
                    "</u></b>. Redirecting to main page in 5 seconds...";
                Timer1.Enabled = true;
            }
            else
            {
                indicationLabel.Text = "Request not successfully saved!";
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Response.Redirect("~/StaffHousekeepingModule/ViewRequest.aspx");
        }

        protected void backToRequestBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/StaffHousekeepingModule/ViewRequest.aspx");
        }
    }
}